package com.sky.item.service.es.impl;

import com.sky.dto.DishPageQueryDTO;
import com.sky.item.repository.Dishrepository;
import com.sky.item.service.es.DishVOForES;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishVOForESImpl implements DishVOForES {

    @Autowired
    private Dishrepository dishrepository;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    ElasticsearchOperations elasticsearchOperations;
    @Override
    public void saveAll(List<DishVO> dishVOList) {
        dishrepository.saveAll(dishVOList);
    }

    @Override
    public void deleteAll() {
        dishrepository.deleteAll();
    }

    @Override
    public DishVO findById(Long id) {
        Optional<DishVO> dishVOOptional = dishrepository.findById(Integer.valueOf(id.toString()));
        DishVO dishVO = dishVOOptional.get();
        return dishVO;
    }


    /**
     * 分页查询ES
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public Page<DishVO> findPage(DishPageQueryDTO dishPageQueryDTO) {
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        if (dishPageQueryDTO.getName()!=null&& !dishPageQueryDTO.getName().isEmpty()){
            queryBuilder.filter(QueryBuilders.matchPhraseQuery("name", dishPageQueryDTO.getName()));
        }
        if(dishPageQueryDTO.getCategoryId()!=null){
            queryBuilder.filter(QueryBuilders.termQuery("categoryId", dishPageQueryDTO.getCategoryId()));
        }
        if (dishPageQueryDTO.getStatus()!=null){
            queryBuilder.filter(QueryBuilders.termQuery("status", dishPageQueryDTO.getStatus()));
        }
        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .withSort(Sort.by(Sort.Order.desc("updateTime")))
                .build();
        SearchHits<DishVO> searchHits = elasticsearchOperations.search(nativeSearchQuery, DishVO.class);
        List<DishVO> dishVOList = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
        Page<DishVO> dishVOPage = new PageImpl<>(dishVOList, PageRequest.of(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize()), searchHits.getTotalHits());
//        Page<DishVO> dishVOPage = new PageImpl<DishVO>(dishVOList, nativeSearchQuery.getPageable(), searchHits.getTotalHits());

        return dishVOPage;
    }
}
